<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <title>Polyfill.js Demo &mdash; position: sticky</title>
    <link type="text/css" href="../../css/demos.css" rel="stylesheet">
    <link id="position-sticky" type="text/css" href="style.css" rel="stylesheet">
    <script>
      (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
      (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
      m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
      })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
      ga('create', 'UA-40829935-1', 'github.io');
      ga('send', 'pageview');
    </script>
  </head>
  <body>

    <div class="header">
      <div class="center">
        <h1 class="header-title">Demo: Position Sticky</h1>
      </div>
    </div>

    <div class="nav">
      <div class="center">Top navigation bar <em>(this will stick when scrolling)</em></div>
    </div>

    <div class="center">

      <div class="sidebar">
        <h2>Sidebar</h2>
        <p>This sidebar will stick when scrolling as long as the window is larger than <code>40em</code>.</p>
        <p>When smaller than <code>40em</code> the sidebar appear in the normal flow above the main content.</p>
        <p><a href="">View Demo Source</a></p>
      </div>

      <div class="content">

        <h2>What Is Position Sticky?</h2>

        <p><code>position: sticky</code> is a newly proposed feature of CSS that allows you to conditionally position elements based on the user's scroll position. Elements that have a position value of <code>sticky</code> will remain in the normal flow until their <code>top</code>, <code>right</code>, <code>bottom</code>, or <code>left</code> value are less than or equal to the users scroll position in that direction. After that condition is met the element "sticks" in place, behaving very similar to <code>position: fixed</code>.</p>

        <p>Paraphrasing from Edward O’Connor's original <a href="http://lists.w3.org/Archives/Public/www-style/2012Jun/0627.html">proposal</a> of this feature:
        </p>

        <blockquote>Many web sites have elements that alternate between being in-flow and having <code>position: fixed</code>, depending on the user's scroll position. This is often done for elements in a sidebar that the page author wants to be always visible as the user scrolls, but which slot into a space on the page when scrolled to the top. Good examples are <a href="https://news.google.com/">news.google.com</a> (the "Top Stories" sidebar) and yelp.com (<a href="http://www.yelp.com/search?find_desc=restaurants&amp;find_loc=San+Francisco%2C+CA&amp;ns=1">search results map</a>).</blockquote>

        <p>For a more detailed look at <code>position: sticky</code>, this <a href="http://updates.html5rocks.com/2012/08/Stick-your-landings-position-sticky-lands-in-WebKit">HTML5Rocks</a> is very informative.</p>

        <h2>What to Look For In This Demo</h2>

        <p class="alert alert-success">Scroll down to see the demo in action, and resize the window to see how it responds at different media values.</p>

        <p>The top navigation has the following declaration in the CSS:</p>

<pre><code>.navbar {
  position: sticky;
  top: 0;
}</code></pre>

        <p>This means that the navigation should be in the normal flow up until the point when the user scrolls the page such that the navigation is zero pixels from the top. Once that point is reached, the navigation bar should "stick" in place until the user scrolls back in the opposite direction.</p>

        <p>The sidebar's <code>position: sticky</code> declaration is in a media query and should only apply when the browser window is smaller than <code>40em</code>. Here is what that CSS looks like:</p>

<pre><code>@media (min-width: 40em) {
  .sidebar {
    position: sticky;
    top: 82px;
    float: right;
  }
}</code></pre>

        <p>When the window is wider than <code>40em</code> the sidebar should be floated to the right, and when the user has scrolled such that the sidebar is less than or equal to 82 pixels from the top of the page. It will stick in place.</p>

        <p>If at any point the window becomes smaller than <code>40em</code>, even if the sidebar is currently in sticky mode, the entire declaration should be ignored and the sidebar should return to the normal flow.</p>

        <p>The complete HTML, CSS, and JavaScript code is available in the <a href="https://github.com/philipwalton/polyfill/tree/master/demos/position-sticky">position-sticky demo folder of the Polyfill.js Github repository</a>.</p>

        <h2>Limitations</h2>

        <p>There is currently no W3c specification for the position value <code>sticky</code>, as such this polyfill is limited in scope. It is also primarily for demonstration purposes, and should not be considered ready for production code.</p>

        <p>For example, only the <code>top</code> value is taken into consideration (not <code>right</code>, <code>bottom</code>, or <code>left</code>) and pixels are the only unit supported.</p>

        <p>In addition, Chrome's implementation of <code>position: sticky</code> confines an element to within the bounds of its parent, which this polyfill does not attempt to do.</p>

        <h2>Keep Scrolling...</h2>

        <p>In order to see the scrolling in action, the page needs to be tall enough to scroll, do here's our old friend Lorem Ipsum</p>
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut et ligula lorem, eu dapibus nulla. Donec cursus interdum ipsum, sit amet cursus felis rhoncus eu. Duis sollicitudin dignissim sapien, ac placerat dolor rutrum vitae. In eu orci nunc, vitae feugiat neque. Fusce sed est vitae felis fringilla elementum. Aenean tortor justo, facilisis eu tempus non, hendrerit id nunc. Etiam blandit odio eget metus sollicitudin aliquam. Nam nunc sem, lacinia eget sollicitudin bibendum, suscipit vel arcu. Nullam in volutpat justo. Etiam non risus felis, vitae volutpat nisi. Morbi libero orci, porta vitae blandit quis, tristique ut risus. Aenean condimentum dictum dolor eu cursus.</p>
        <p>Nulla sit amet odio leo, non vulputate massa. Aliquam et condimentum risus. Donec id enim nibh, vitae placerat dui. In hac habitasse platea dictumst. Vivamus ac nulla turpis, vitae aliquam dui. Sed malesuada commodo urna, vel ornare dolor mattis at. Mauris neque mi, fringilla nec sodales eu, accumsan sed purus.</p>
        <p>Fusce ultrices, nisl accumsan pharetra scelerisque, tellus ante facilisis libero, varius dapibus lacus odio eu velit. Curabitur eu arcu vel neque vehicula eleifend aliquam a erat. In hac habitasse platea dictumst. Morbi elementum enim quis lorem suscipit sit amet aliquet tellus condimentum. In ullamcorper elit sit amet ipsum cursus in imperdiet arcu porttitor. Praesent eu est quam, a pretium ante. Duis et nulla tellus. Phasellus varius blandit eros sit amet porttitor. Etiam sodales, ligula eget porttitor commodo, quam metus tempus nisi, nec iaculis erat arcu eget leo. Maecenas augue eros, tempus ut iaculis at, rhoncus in tellus.</p>
        <p>Phasellus dui justo, egestas non varius eu, fringilla a purus. Proin sodales tempus augue id posuere. Vivamus posuere facilisis sapien et pellentesque. Etiam quis facilisis urna. Nam vehicula justo vel mauris sagittis lacinia. Ut egestas metus nec risus suscipit id congue nisl elementum. Vivamus rutrum augue sit amet quam egestas vitae dictum nisi accumsan. Fusce ante dui, faucibus rutrum semper sit amet, varius et odio. Aenean auctor tortor non felis tristique bibendum. Quisque sem tortor, tempor sed posuere vel, vehicula a tortor. Praesent quam massa, vulputate sit amet accumsan eget, cursus ac neque. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Suspendisse potenti.</p>
        <p>Nulla sit amet odio leo, non vulputate massa. Aliquam et condimentum risus. Donec id enim nibh, vitae placerat dui. In hac habitasse platea dictumst. Vivamus ac nulla turpis, vitae aliquam dui. Sed malesuada commodo urna, vel ornare dolor mattis at. Mauris neque mi, fringilla nec sodales eu, accumsan sed purus.</p>
        <p>Fusce ultrices, nisl accumsan pharetra scelerisque, tellus ante facilisis libero, varius dapibus lacus odio eu velit. Curabitur eu arcu vel neque vehicula eleifend aliquam a erat. In hac habitasse platea dictumst. Morbi elementum enim quis lorem suscipit sit amet aliquet tellus condimentum. In ullamcorper elit sit amet ipsum cursus in imperdiet arcu porttitor. Praesent eu est quam, a pretium ante. Duis et nulla tellus. Phasellus varius blandit eros sit amet porttitor. Etiam sodales, ligula eget porttitor commodo, quam metus tempus nisi, nec iaculis erat arcu eget leo. Maecenas augue eros, tempus ut iaculis at, rhoncus in tellus.</p>
        <p>Phasellus dui justo, egestas non varius eu, fringilla a purus. Proin sodales tempus augue id posuere. Vivamus posuere facilisis sapien et pellentesque. Etiam quis facilisis urna. Nam vehicula justo vel mauris sagittis lacinia. Ut egestas metus nec risus suscipit id congue nisl elementum. Vivamus rutrum augue sit amet quam egestas vitae dictum nisi accumsan. Fusce ante dui, faucibus rutrum semper sit amet, varius et odio. Aenean auctor tortor non felis tristique bibendum. Quisque sem tortor, tempor sed posuere vel, vehicula a tortor. Praesent quam massa, vulputate sit amet accumsan eget, cursus ac neque. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Suspendisse potenti.</p>
      </div>
    </div>

    <script src="../../lib/jquery.js"></script>
    <script src="../../dist/polyfill.js"></script>
    <script src="polyfill.position-sticky.js"></script>
  </body>
</html>